package com.home.biz;

import com.github.pagehelper.PageHelper;
import com.home.base.biz.BaseBiz;
import com.home.base.exceptions.ServiceException;
import com.home.base.util.FetchCoinUtil;
import com.home.config.InitDataConfig;
import com.home.entity.CoinResult;
import com.home.entity.FetchPhone;
import com.home.entity.JobUser;
import com.home.entity.ProvInfo;
import com.home.mapper.FetchPhoneMapper;
import com.home.mapper.JobUserMapper;
import com.home.task.FetchTask;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：chenxf
 * @date ：Created in 2020/3/18 12:46
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class PhoneBiz extends BaseBiz<FetchPhoneMapper, FetchPhone> {

    @Autowired
    ProvBiz provBiz;
    @Autowired
    FetchTask fetchTask;

    /**

     * @param limit  条数
     * @param offset  页码
     * @return
     */
    public List<CoinResult> list(int limit, int offset, boolean isSuccess){
        if(StringUtils.isEmpty(InitDataConfig.getToken())){
            return new ArrayList<>();
        }
        try {
            return new FetchCoinUtil(InitDataConfig.getToken()).orders(isSuccess);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();

    }




    public List<Map> queryCityList(){
        return mapper.selectCity();
    }

    public void fetch(String sNum, String eNum,String token,String veryCode,String price,String num){
        //线程开始采集
       if(StringUtils.isEmpty(token)||StringUtils.isEmpty(sNum)||StringUtils.isEmpty(eNum)){
           throw new ServiceException("参数错误");
       }
       InitDataConfig.setToken(token);
       InitDataConfig.setStartTime(sNum);
       InitDataConfig.setEndTime(eNum);
       InitDataConfig.setVeryCode(veryCode);
       InitDataConfig.setPrice(price);
       InitDataConfig.setNum(Integer.valueOf(num));

        //结束
    }

    public void deletePhone(){
        mapper.deletePhone();
    }

    public Integer counts(){
        return mapper.phoneCounts();
    }

    public void saveList(String city,String province,String method,List<String> phone) {
        if(phone==null){
            return;
        }
        for(String p:phone){
            save(FetchPhone.builder().cityName(city).provName(province).fetchMethod(method).phoneNum(p).build());
        }

    }

    public void save(FetchPhone fetchPhone) {

        try {
            insertSelective(fetchPhone);
        } catch (Exception e) {
            //e.printStackTrace();
        }

    }
}
